package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Role;
import cn.zj.logistics.pojo.RoleExample;
import cn.zj.logistics.pojo.RoleExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.RoleService;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	@RequiresPermissions("role:rolePage")
	@RequestMapping("/rolePage")
	public String rolePage() {

		return "rolePage";
	}
	
	@RequiresPermissions("role:list")
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<Role> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		RoleExample example = new RoleExample();

		// 前台传递关键字参数不为空的时候才参与条件查询
		if (StringUtils.isNotBlank(keyword)) {
			Criteria criteria1 = example.createCriteria();
			criteria1.andRolenameLike("%" + keyword + "%");
		}

		List<Role> roles = roleService.selectByExample(example);
		
		PageInfo<Role> pageInfo = new PageInfo<>(roles);

		return pageInfo;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long roleId) {
		
		
		/*
		 * 删除之前先要去管理员表根据删除角色id作为条件查询是否拥有数据
		 * 有：说明当前角色还关联有管理员，不能删除
		 * 没有：可以删除
		 */
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
	
		criteria.andRoleIdEqualTo(roleId);
		
		List<User> users = userService.selectByExample(example);
		
		if(users.size()>0) {
			return MessageObject.createMo(0, "此角色还管理有管理员，不能删除");
		}
		

		int row = roleService.deleteByPrimaryKey(roleId);

		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(Role role) {

		int row = roleService.insert(role);

		MessageObject mo = MessageObject.createMo(0, "新增数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "新增数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Role role) {

		int row = roleService.updateByPrimaryKeySelective(role);

		MessageObject mo = MessageObject.createMo(0, "修改数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "修改数据成功");
		}
		return mo;
	}

	@RequestMapping("/roleEdit")
	public String roleEdit(Model m, Long roleId) {


		if (roleId != null) {
			// 查询出当前要修改的用户信息共享给编辑页面以供修改数据回显
			Role role = roleService.selectByPrimaryKey(roleId);
			m.addAttribute("role", role);
		}
		

		return "roleEdit";
	}



}
